package com.jianshuge.app.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jianshuge.app.AppException;
import com.jianshuge.app.bean.MyBookDetail.Reply;
import com.jianshuge.app.bean.MyBooklistDetail.MyBooklistItem;
import com.jianshuge.app.common.StringUtils;

public class DoubanBook extends Entity {
	
	private String  id;
	private String  isbn; 
	private String  title;
	private String  author;
	private String  pubdate;
	private String  publisher;
	private String  summary ;
	private String  keywords; 
	private String  image_url; 
	private String  subject_id;
	private String  rating; 

	
	public static class Reply implements Serializable{
		public String rauthor;
		public String rpubDate;
		public String rcontent;
	}
	
	public String getIsbn( ) {
		return isbn;
	}
	
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}
	
	public void setAuthor(String author) {
		this.author = author;
	}
	
	public String getPubdate() {
		return pubdate;
	}
	
	public void setPubdate(String pubdate) {
		this.pubdate = pubdate;
	}
	
	public String getPublisher() {
		return publisher;
	}
	
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	
	public String getSummary() {
		return summary;
	}
	
	public void setSummary(String summary) {
		this.summary = summary;
	}
	
	public String getkeywords() {
		return keywords;
	}
	
	public void setkeywords(String keywords) {
		this.keywords = keywords;
	}
	
	public String getImageUrl() {
		return image_url;
	}
	public void setImageUrl(String image_url) {
		this.image_url = image_url;
	}
	
	public String getSubjectId() {
		return subject_id;
	}
	
	public void setSubjectId(String subject_id) {
		this.subject_id = subject_id;
	}

	public String getRating() {
		return rating;
	}
	
	public void setRating(String rating) {
		this.rating = rating;
	}
	
	public static DoubanBook parse(InputStream inputStream) throws IOException, AppException {
		
		DoubanBook doubanbook = new DoubanBook();

		String inputStr;
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream) ); 
	    StringBuilder responseStrBuilder = new StringBuilder();
	    while ((inputStr = streamReader.readLine()) != null)
	        responseStrBuilder.append(inputStr);
	    String strResult = responseStrBuilder.toString();
	    
		try {
			
			JSONObject doubanbook_json_obj = new JSONObject(strResult);
			doubanbook.id = doubanbook_json_obj.optString("id");
			doubanbook.setTitle(doubanbook_json_obj.optString("title"));
			
			String Author = "";
			JSONArray author_json_arr = doubanbook_json_obj.optJSONArray("author");
			if(author_json_arr != null) {
				for (int k = 0; k < author_json_arr.length(); k++) {
					Author += author_json_arr.get(k) + " ";
				}
				doubanbook.setAuthor(Author);
			}
			
			doubanbook.setPubdate(doubanbook_json_obj.getString("pubdate"));
			doubanbook.setPublisher(doubanbook_json_obj.getString("publisher"));

			doubanbook.setImageUrl(doubanbook_json_obj.optString("image"));
			doubanbook.setSubjectId(doubanbook_json_obj.optString("id"));
			
			JSONObject rating_json = doubanbook_json_obj.optJSONObject("rating");
			doubanbook.setRating( rating_json.optString("average") );
			
			doubanbook.setIsbn(doubanbook_json_obj.optString("isbn13"));

		//	doubanbook.follow_status = doubanbook_json_obj.getBoolean("follow_status");
			/***
			String keywords = "";
			JSONArray tag_json_arr = doubanbook_json_obj.optJSONArray("tags");
			if(tag_json_arr != null) {		
				for (int j = 0; j < tag_json_arr.length(); j++) {
					JSONObject tag_json_obj = tag_json_arr.getJSONObject(j);
					keywords += tag_json_obj.optString("title") + ",";
				}
				doubanbook.setkeywords(keywords);
			}
			***/
			
			

			
		} catch (JSONException e) { 
            System.out.println("Jsons parse error !"); 
            e.printStackTrace(); 
        }
		
	    return doubanbook;
	}
	
}
